package first;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import first.daodto.LiteratureDao;
import first.daodto.DaoImpl;
import first.libxml.LitValidXSD;
import first.libxml.LitXml;
import first.liter.AbstractLanguage;
import first.liter.BaseLanguage;
import first.liter.Literature;
import first.users.BiblioUser;
import first.users.Librarian;
import first.users.Reader;

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.sql.*;


class AmOfPgsComparator implements Comparator<Integer>
{
	private List<Literature> lrt;
	
	public AmOfPgsComparator(List<Literature> lit)
	{
		this.lrt=new ArrayList<Literature>();


		for(Literature s:lit)
		{
			lrt.add(s);
		}
	}
	
	public int compare(Integer o1, Integer o2) {
		
		return (Integer)this.lrt.get(o1).getAmountOfPages().compareTo(this.lrt.get(o2).getAmountOfPages());

	}
}

	class IdsComparator implements Comparator<Integer>
	{
		private List<Literature> lrt;
		
		public IdsComparator(List<Literature> lit)
		{
			this.lrt=new ArrayList<Literature>();
		/*	Collections.copy(this.lrt,lit);
		*/
		
			for(Literature s:lit)
			{
				lrt.add(s);
			}
		}
		
		public int compare(Integer o1, Integer o2) {
			
			return (Integer)this.lrt.get(o1).getIdLit().compareTo(this.lrt.get(o2).getIdLit());
		}
	
	

/*	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		return 0;
	}*/
	
}


/**
 * Main, executing class
 * 
 * @author Kozlov Kirill
 * @version Thu Oct 21,2010
 * 
 */
public class Main 
{
	
	//private DaoImpl DI=new DaoImpl();

	public static void main(String[] args)
	{
		System.out.println("Hello! ^_^");
		
		
		LitValidXSD.check("data//literatures.xml");
		
		//List<Literature> ltr=LitXml.setLitFromXmlSAX();
		List<Literature> ltr=LitXml.setLitFromXmlDOM();
		
		for(Literature a:ltr)
		{
			System.out.println("\n после LitXml");
			System.out.println(a.toString());
		}
		
		//LitXml.pushListOfLitFromXmlToDBSAX();
		//LitXml.deleteListOfLitFromXmlToDBSAX();
		
		System.out.println("\nEureka! I've readed from xml!\n");
		/*
		System.out.println("Hello people!!");
		Librarian lb1 = new Librarian("Vasya", "123321");
		
		BaseLanguage bas1=AbstractLanguage.getLanguage("english");
		Literature book=bas1.createBooks("Voina i mir");
		System.out.println("book="+book.getLanguageLit()+" результат примера метода ="+book.someMethod());
		
		Catalogue cat=new Catalogue();
		
	//	cat.addLiterature(book);
		 
		BaseLanguage bas2=AbstractLanguage.getLanguage("ukrainian");
		
		Literature book1=bas1.createBooks("Voina i mir");
		Literature book2=bas2.createBooks("Taras Bulba");
		
	//	cat.addLiterature(book1);
	//	cat.addLiterature(book2);
		*/
		final DaoImpl DaoI=new DaoImpl();
		
		Connection conn=null;
		try {
			conn = DriverManager.getConnection("jdbc:h2:db/kozlov_biblio","sa","");

		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		finally
		{
			if (conn!=null)
			{			
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
			}
		}
		
		try {
			DaoI.start();
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		BaseLanguage d=AbstractLanguage.getLanguage("ukrainian");
		Literature jour=d.createJournals("Комсомольська правда");
		jour.setAuthor("КС Украина");
		jour.setAvailabilityOfLit(false);
		jour.setAmountOfPages(35);
		
		Library df=new Library("Проверка");
		df.setDescrLibrary("Проверка");
		df.setIdLibrary(25);
		
		
		Reader we=new Reader("pash", "idn", "1244", 2);
		
		int t;
		try {
			t=DaoI.createLibrary(df);
			t=DaoI.createLiterature(jour);
			t=DaoI.deleteOneLiterature(jour);
			//t=DaoI.createUser(we, df);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		List<Literature> litt=null;
		
		List<Library> libb=null;
		
		List<BiblioUser> lius=null;
		
		try
		{
			//lius=DaoI.findAllLibraryUsers(df);
			System.out.println("1");
		//	libb=DaoI.findAllLibrariesSimple();
			System.out.println("2");
			litt=DaoI.findAllLiteraturesSimple();
			System.out.println("3");
			for(Literature rt:litt)
			{
				System.out.println("Literature name ="+rt.getNameLit()+" id="+rt.getIdLit());
			}
		/*	
			System.out.println("MY Lybraries:");
			for(Library rb:libb)
			{
				System.out.println("name="+rb.getNameLibrary()+" id="+rb.getIdLibrary()+"  desc="+rb.getDescrLibrary());
			}
			*/
//			for(BiblioUser rt:lius)
//			{
//				System.out.println("user name="+rt.getName()+" id="+rt.getId());
//			}
			
		}
		catch(SQLException e)
		{
			
		}
		
		List<Integer> listbyIDs=new ArrayList<Integer>();
		List<Integer> listbyAmOfPgs=new ArrayList<Integer>();
		
		for(Literature a: litt)
		{
			//System.out.println(",jnhkjbnjkb");
			if (litt.indexOf(a)!=-1) listbyIDs.add((Integer)litt.indexOf(a));
			if (litt.indexOf(a)!=-1) listbyAmOfPgs.add((Integer)litt.indexOf(a));
		}
		
		AmOfPgsComparator cmp1=new AmOfPgsComparator(litt);
		IdsComparator cmp2=new IdsComparator(litt);
		
		
		Collections.sort(listbyAmOfPgs, cmp1);
		Collections.sort(listbyIDs, cmp2);
		
		for(Integer q:listbyAmOfPgs)
		{
		//	System.out.println("##"+q.toString());
		}
		
		//Comparator<Integer> compID=
		
		
		
		try {
			DaoI.stop();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
//================================================================================================================

		File ff=new File("/media/B32740CCC92117C1/JavaProjects/test/phithics.catalogue");
		
		System.out.println(ff.isDirectory());
		
		Library LIB=Library.setUpLibrary();
		
		//System.out.println(LIB.getNameLibrary());
		
		Pattern p=Pattern.compile("(\"name\":\")|(\")");
		Matcher m = p.matcher("\"name\":\"Vasya\"");  
		
		
//==================================================================================================
//===================================find Fields of class===========================================		
		
		System.out.println(Library.class.getDeclaredFields().length);
		for(int i=0;i<Library.class.getDeclaredFields().length-1;i++)
		{
			//System.out.println(Library.class.getDeclaredFields()[i].toString());
		
			p=Pattern.compile("first.Library.");
			System.out.println(p.split(Library.class.getDeclaredFields()[i].toString())[1]);
		}

		
		p=Pattern.compile(".catalogue");
		System.out.println("\n"+p.split("phithics.catalogue")[0]);
//==================================================================================================		
//==================================================================================================
	
		
		
		Locale current=Locale.getDefault();
		
		System.out.println(current.getDisplayCountry());
	}
}

